<?php

    class attributes_to_object_model extends CI_Model{

        function __construct()
        {
            parent::__construct();
        }

        function insert($data)
        {
            $this->db->insert('object_attributes',$data);
            return $this->db->insert_id();
        }

        function view_all_attributes($custom = NULL)
        {
            $q = '
            SELECT * from object_attributes
            ';
            if(isset($custom))
            {
                $q.="WHERE custom = $custom";
            }

            $res = $this->db->query($q)->result_array();
            return $res;

        }

        function delete_attribute($attribute_ID)
        {
            $q="
            DELETE FROM object_attributes
            WHERE ID = $attribute_ID
            ";
            $res = $this->db->query($q);
            return $res;
        }

        function get_object_attributes($type,$groupID,$as_array=FALSE)
        {
            $q = "SELECT oa.ID, oa.type, oa.custom, oa.groups_ID, oa.attributes_ID, a.ID as att_ID, a.name, a.value, a.required, a.custom
            FROM object_attributes as oa
            LEFT JOIN attributes as a ON oa.attributes_ID = a.ID
            WHERE oa.type = '$type'
            AND oa.groups_ID = $groupID
            ORDER BY oa.order ASC, oa.custom ASC, oa.ID ASC 
            ";

            $res = $this->db->query($q)->result_array();

            if($as_array==FALSE)
            {
                return $res;
            }
            else
            {
                $my_array = array();
                foreach ($res as $row):
                    $my_array[]=$row['attributes_ID'];
                    endforeach;

                return $my_array;
            }
        }
        
        function get_all_custom_attributes($group_ID)
        {
            $q = '
            SELECT * from attributes
            WHERE custom = 1
            AND groups_ID = '.$group_ID.'
            ';
            $res = $this->db->query($q)->result_array();
            return $res;

        }

    }

?>